<template>
  <div class="dormitory-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>宿舍楼管理</span>
          <el-button type="primary" @click="showAddDialog">添加宿舍楼</el-button>
        </div>
      </template>
      
      <el-table :data="dormitories" style="width: 100%">
        <el-table-column prop="dormitory" label="宿舍楼名称" />
        <el-table-column prop="floors" label="楼层数" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button type="primary" size="small" @click="editDormitory(scope.row)">
              编辑
            </el-button>
            <el-button type="danger" size="small" @click="deleteDormitory(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    
    <!-- 添加/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑宿舍楼' : '添加宿舍楼'" width="500px">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="宿舍楼名称" prop="dormitory">
          <el-input v-model="form.dormitory" />
        </el-form-item>
        <el-form-item label="楼层数" prop="floors">
          <el-input-number v-model="form.floors" :min="1" :max="20" />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm" :loading="submitting">
          确定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '../../utils/request'

const dormitories = ref([])
const dialogVisible = ref(false)
const isEdit = ref(false)
const formRef = ref()
const submitting = ref(false)

const form = reactive({
  dormitory: '',
  floors: 1
})

const rules = {
  dormitory: [{ required: true, message: '请输入宿舍楼名称', trigger: 'blur' }],
  floors: [{ required: true, message: '请输入楼层数', trigger: 'blur' }]
}

const loadDormitories = async () => {
  try {
    const response = await request.get('/api/dormitory/list')
    if (response.data.code === 200) {
      dormitories.value = response.data.data
    }
  } catch (error) {
    console.error('加载宿舍楼数据失败:', error)
  }
}

const showAddDialog = () => {
  isEdit.value = false
  form.dormitory = ''
  form.floors = 1
  dialogVisible.value = true
}

const editDormitory = (row) => {
  isEdit.value = true
  Object.assign(form, row)
  dialogVisible.value = true
}

const deleteDormitory = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除这个宿舍楼吗？', '确认删除', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    const response = await request.delete(`/api/dormitory/delete/${row.dormitory}`)
    if (response.data.code === 200) {
      ElMessage.success('删除成功')
      await loadDormitories()
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

const submitForm = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      submitting.value = true
      try {
        const url = isEdit.value ? '/api/dormitory/update' : '/api/dormitory/add'
        const response = await request.post(url, form)
        
        if (response.data.code === 200) {
          ElMessage.success(isEdit.value ? '更新成功' : '添加成功')
          dialogVisible.value = false
          await loadDormitories()
        }
      } catch (error) {
        ElMessage.error(isEdit.value ? '更新失败' : '添加失败')
      } finally {
        submitting.value = false
      }
    }
  })
}

onMounted(() => {
  loadDormitories()
})
</script>

<style scoped>
.dormitory-management {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 